package com.rbac.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rbac.admin.common.Result;
import com.rbac.admin.model.dto.UserDTO;
import com.rbac.admin.model.vo.UserVO;
import com.rbac.admin.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户控制器
 */
@RestController
@RequestMapping("/api/users")
@Tag(name = "用户管理", description = "用户管理")
public class UserController {

    @Autowired
    private SysUserService userService;

    /**
     * 获取当前用户信息
     * @return 用户信息
     */
    @Operation(summary = "获取当前用户信息")
//    @PreAuthorize("hasAuthority('user:query')")
    @GetMapping("/getCurrentUser/{userId}")
    public Result<UserVO> getCurrentUserInfo(@PathVariable("userId") Long userId) {
        UserVO userVO = userService.getUserInfo(userId);
        return Result.success(userVO);
    }

    /**
     * 更新用户信息
     * @param userDTO 用户信息
     * @return 更新结果
     */
    @Operation(summary = "更新用户信息")
    @PutMapping("/updateUser")
//    @PreAuthorize("hasAuthority('user:edit')")
    public Result<Boolean> updateUserInfo(@Validated @RequestBody UserDTO userDTO) {
        boolean result = userService.updateUserInfo(userDTO);
        return Result.success(result);
    }

    /**
     * 分页查询用户列表
     * @param pageNum 页码
     * @param pageSize 每页数量
     * @param keyword 关键词
     * @return 分页结果
     */
    @Operation(summary = "分页查询用户列表")
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('sys:user:list')")
    public Result<Page<UserVO>> listUserPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String keyword) {
        Page<UserVO> page = userService.listUserPage(pageNum, pageSize, keyword);
        return Result.success(page);
    }

    /**
     * 获取用户角色ID列表
     * @param userId 用户ID
     * @return 角色ID列表
     */
    @Operation(summary = "获取用户角色ID列表")
    @GetMapping("/getRoleIds/{userId}")
    @PreAuthorize("hasAuthority('sys:user:assign')")
    public Result<List<Long>> getRoleIdsByUserId(@PathVariable("userId") Long userId) {
        List<Long> roleIds = userService.getRoleIdsByUserId(userId);
        return Result.success(roleIds);
    }

    /**
     * 分配用户角色
     * @param userId 用户ID
     * @param roleIds 角色ID列表
     * @return 分配结果
     */
    @Operation(summary = "分配用户角色")
    @PostMapping("/roles/{userId}")
    @PreAuthorize("hasAuthority('sys:user:assign')")
    public Result<Boolean> assignRoles(
            @PathVariable("userId") Long userId,
            @RequestBody List<Long> roleIds) {
        boolean result = userService.assignRoles(userId, roleIds);
        return Result.success(result);
    }
}